package com.elite.controller;

import com.alibaba.fastjson.JSON;
import com.elite.entity.Goods;
import com.elite.service.GoodsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by hbl on 2017/2/4.
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
    //引入log4j进行打印
    private static final Logger LOGGER = Logger.getLogger(UserController.class);
    @Autowired
    private GoodsService goodsService;

//    将一条商品信息保存到数据库，前端做是否为空的判断，这里就不判断了
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    public String insert(Integer sellerId,String goodsName,Float goodsPrice,String goodsIntroducts,String goodsCity,String goodsSchool,Integer goodsSize,String goodsUnit,String goodCategory){
        Goods goods = new Goods();
        goods.setSellerId(sellerId);
        goods.setGoodsName(goodsName);
        goods.setGoodsPrice(goodsPrice);
        goods.setGoodsIntroducts(goodsIntroducts);
        goods.setGoodsCity(goodsCity);
        goods.setGoodsSchool(goodsSchool);
        goods.setGoodsSize(goodsSize);
        goods.setGoodsUnit(goodsUnit);
        goods.setGoodCategory(goodCategory);
        goodsService.addGoods(goods);
        return JSON.toJSON(goods).toString();
    }

    @RequestMapping(value = "/selectBySeller",produces = "text/html;charset=UTF-8")
    public String selectBySeller(Integer sellerId){
        List<Goods> goodses = goodsService.selectGoodsBySellterId(sellerId);
        return JSON.toJSON(goodses).toString();
    }

    @RequestMapping(value = "/change",method = RequestMethod.POST)
    public void change(Integer goodsId,Float goodsPrice,String goodsDanwei,Integer goodsNum){
        Goods goods = new Goods();
        goods.setGoodsId(goodsId);
        goods.setGoodsPrice(goodsPrice);
        goods.setGoodsUnit(goodsDanwei);
        goods.setGoodsSize(goodsNum);
        goodsService.changeGoods(goods);
    }

}
